#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
	int s;
	int fd;
	int len;
	int chnum = 0;
	int sin_size;
	char buf[BUFSIZ];
	char cmdbuf[BUFSIZ];
	char fpath[64] = "/data/mtinfo";
	FILE *fp;
	struct sockaddr_in my_addr;
	struct sockaddr_in remote_addr;

	memset(&my_addr,0,sizeof(my_addr));

	my_addr.sin_family=AF_INET;
	my_addr.sin_addr.s_addr=INADDR_ANY;
	my_addr.sin_port=htons(9527);

	if((s=socket(AF_INET,SOCK_STREAM,0))<0)
	{
		perror("socket");
		return 1;
	}
	if (bind(s,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0)
	{
		perror("bind");
		return 1;
	}
	listen(s,5);

	sin_size=sizeof(struct sockaddr_in);
	
	if((fd=accept(s,(struct sockaddr *)&remote_addr,&sin_size))<0)
	{
		perror("accept");
		return 1;
	}

	printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr));
	len=send(fd,"Welcome to my server\n",21,0);

	while((len=recv(fd,buf,BUFSIZ,0))>0)
	{
		buf[len]='\0';
		printf("%s\n",buf);
		sprintf(cmdbuf,"%s 2> %s",buf,fpath);
		
		system(cmdbuf);
		fp = fopen(fpath,"r");
		
		if(fgets(cmdbuf,sizeof(buf),fp))//cmd failed
		{
		   printf("----->cmd failed\n");
		   strcpy(cmdbuf,"fail");
		   if(send(fd,cmdbuf,sizeof(cmdbuf),0)<0)
		   {
			perror("send error");
			return 1;
		   }		
		}
		else//successed
		{
			printf("----->cmd successed\n");
			strcpy(cmdbuf,"ok");
			if(send(fd,cmdbuf,sizeof(cmdbuf),0)<0)
			{
				perror("send error");
				return 1;	
			}		
		}
			 
		
	}
	close(fd);
	//close(s);
	return 0;

}
